package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = Protocol.C_UNION_CROSS_CHANGE_RANK_GRADE_JOIN_LIMIT, desc = "修改实力评定加入限制")
public class CrossChangeRankGradeJoinLimitCmd extends CrossCommand {


    long playerId;


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        UnionProto.ChangeUnionRankGradeJoinLimitReqMsg reqMsg = UnionProto.ChangeUnionRankGradeJoinLimitReqMsg.parseFrom(packet.getBytes());

        CrossUserBaseInfo crossUserBaseInfo = CrossUserMgr.getCrossUserBaseInfo(packet.getPlayerId());
        if (crossUserBaseInfo == null) {
            ret = GameErrorCode.E_USER_NO_FOUND;
            return;
        }

        CrossUnionMember member = CrossUnionMgr.getUnionMember(crossUserBaseInfo.getUserBaseInfo().getUnionUid(), playerId);
        if (member == null || StringUtils.isNullOrEmpty(member.getUnionUid())) {
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(member.getUnionUid());
        if (unionInfo == null) {
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        if (member.getPosition() == eUnionPosition.Master.getValue()
                || member.getPosition() == eUnionPosition.DeputyMaster.getValue()) {
            Map<Integer, Integer> joinLimitMap = new ConcurrentHashMap<>();
            for (UnionProto.UnionRankGradeJoinLimitTemp joinLimitTemp : reqMsg.getNewJoinLimitList()) {
                joinLimitMap.put(joinLimitTemp.getGradeId(), joinLimitTemp.getNum());
            }
            ret = CrossUnionMgr.changeRankGradeJoinLimit(unionInfo, joinLimitMap);
            if (0 == ret) {
                UnionProto.ChangeUnionRankGradeJoinLimitRespMsg.Builder respMsg = UnionProto.ChangeUnionRankGradeJoinLimitRespMsg.newBuilder();
                respMsg.setRet(0);
                respMsg.addAllNewJoinLimit(CrossUnionMgr.getMemberRankGradeMsg(unionInfo));
                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_CHANGE_RANK_GRADE_JOIN_LIMIT, respMsg));
            }
        } else {
            ret = GameErrorCode.E_UNION_POSITION_NO_ENOUGH;
        }

    }

    @Override
    public void sendData() {
        if (0 != ret) {
            UnionProto.ChangeUnionRankGradeJoinLimitRespMsg.Builder respMsg = UnionProto.ChangeUnionRankGradeJoinLimitRespMsg.newBuilder();
            respMsg.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_CHANGE_RANK_GRADE_JOIN_LIMIT, respMsg));
        }
    }
}
